import Vue from 'vue'
import Router from 'vue-router'
import Home from '@/views/index'
// import store from '@/store'
const Index = () => import(/* webpackChunkName: 'index' */ './views/Home/index.vue')
const Template1 = () => import(/* webpackChunkName: 'Template1' */ './views/Home/Template1.vue')

const Login = () => import(/* webpackChunkName: 'login' */ './views/Login.vue')
const Reg = () => import(/* webpackChunkName: 'reg' */ './views/Reg.vue')
const Forget = () => import(/* webpackChunkName: 'forget' */ './views/Forget.vue')
const NotFound = () => import(/* webpackChunkName: 'NotFound' */ './views/NotFound.vue')
const Details = () => import(/* webpackChunkName: 'Details' */ './views/Details.vue')
const PostEdit = () => import(/* webpackChunkName: 'PostEdit' */ './views/PostEdit.vue')

const LeftMenu = () => import(/* webpackChunkName: 'LeftMenu' */ './components/LeftMenu.vue')
const UserIndex = () => import(/* webpackChunkName: 'Home' */ './views/user/Home.vue')
const UserCenter = () => import(/* webpackChunkName: 'UserCenter' */ './views/user/UserCenter.vue')
const Setting = () => import(/* webpackChunkName: 'Setting' */ './views/user/Setting.vue')
const Message = () => import(/* webpackChunkName: 'Message' */ './views/user/Message.vue')

const UesrInfo = () => import(/* webpackChunkName: 'UesrInfo' */ './views/user/UserCommon/UserInfo.vue')
const Pic = () => import(/* webpackChunkName: 'Pic' */ './views/user/UserCommon/Pic.vue')
const Password = () => import(/* webpackChunkName: 'Password' */ './views/user/UserCommon/Password.vue')
const Account = () => import(/* webpackChunkName: 'Account' */ './views/user/UserCommon/Account.vue')

const MyPosts = () => import(/* webpackChunkName: 'MyPosts' */ './views/user/UserCommon/MyPosts.vue')
const MyCollection = () => import(/* webpackChunkName: 'MyCollection' */ './views/user/UserCommon/MyCollection.vue')

Vue.use(Router)

export default new Router({
  mode: 'history',
  routes: [
    {
      path: '/',
      component: Home,
      children: [
        {
          path: '',
          name: 'index',
          component: Index
        },
        {
          path: '/index/:catalog',
          name: 'catalog',
          component: Template1
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    {
      path: '/reg',
      name: 'reg',
      component: Reg,
      beforeEnter: (to, from, next) => {
        if (from.name === 'login') {
          next()
        } else {
          next('/login')
        }
      }
    },
    {
      path: '/forget',
      name: 'forget',
      component: Forget
    },
    {
      path: '/404',
      name: 'not-found',
      component: NotFound
    },
    {
      path: '/details',
      name: 'details',
      component: Details
    },
    {
      path: '/edit',
      name: 'postEdit',
      component: PostEdit
    },
    {
      path: '/user/index',
      name: 'user-index',
      component: UserIndex
    },
    {
      path: '/user',
      name: 'user',
      component: LeftMenu,
      redirect: { name: 'user-index' },
      children: [
        {
          path: 'message',
          name: 'message',
          component: Message
        },
        {
          path: 'center',
          name: 'UserCenter',
          component: UserCenter,
          children: [
            {
              path: '',
              name: 'MyPosts',
              component: MyPosts
            },
            {
              path: 'collection',
              name: 'collection',
              component: MyCollection
            }
          ]
          // 进入之前判断用户是否登录
          // beforeEnter: (to, from, next) => {
          //   console.log(to, 'to')
          //   console.log(from, 'from')
          //   const isLogin = store.state.isLogin
          //   if (isLogin) {
          //     next()
          //   } else {
          //     const userInfo = JSON.parse(localStorage.getItem('setUserInfo'))
          //     const token = JSON.parse(localStorage.getItem('setToken'))
          //     console.log(userInfo, token, 'kk')
          //     if (token !== null && userInfo !== null) {
          //       store.commit('setToken', token)
          //       store.commit('setUserInfo', userInfo)
          //       store.commit('setIsLogin', true)
          //       next()
          //     } else {
          //       next('/login')
          //     }
          //   }
          // }
        },
        {
          path: 'setting',
          name: 'setting',
          component: Setting,
          children: [
            {
              path: '',
              name: 'info',
              component: UesrInfo
            },
            {
              path: 'pic',
              name: 'pic',
              component: Pic
            },
            {
              path: 'password',
              name: 'password',
              component: Password
            },
            {
              path: 'account',
              name: 'account',
              component: Account
            }
          ]
        }
      ]
    }
  ]
})
